package util;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import exception.DAOSystemException;

import bizlogic.HolidayCalendarManager;

import model.HolidayCalendarDTO;
/*
 * @pranjal
 */
public class Validation {
	
	
	public static String validate(HttpServletRequest req, HttpServletResponse resp) {
		String validationmsg="ERROR:";
			
		 Map m = Keyword.m;
		 
		System.out.println("inside validation class");
		
        for (String fieldname : Collections.list((Enumeration<String>)req.getParameterNames()))
        {
    		//System.out.println(name);       
        	if((req.getParameter(fieldname)==null || req.getParameter(fieldname)=="") && !fieldname.equalsIgnoreCase("id"))
			{ String msg="Some fields";
        			if(m.get(fieldname) != null)
        				msg = (String) m.get(fieldname);
        		
				validationmsg += msg  + " cannot be left empty \n";
			}
        	
        	if(fieldname=="name")
        	{
        		if(req.getParameter(fieldname)!=null)
        		{
        			String i = req.getParameter(fieldname);
        			try
        			{
        				Integer.parseInt(i);
        				validationmsg += m.get(fieldname)  + " cannot be number \n";
        			}
        			catch(Exception e)
        			{
        				
        			}
        		}
        	}
      	           
        }
        
        return validationmsg;
	}
	
	public static boolean  isHolidayDate(Date d1) throws DAOSystemException
	{
		
		HolidayCalendarManager holMan = new HolidayCalendarManager();
		ArrayList<HolidayCalendarDTO> holList = holMan.findAllHolidayCalendar();
		for(HolidayCalendarDTO h1 : holList)
		{
		 if(d1.getYear()==h1.getDate().getYear() 
				 && d1.getMonth()==h1.getDate().getMonth() 
				 && d1.getDay()==h1.getDate().getDay())
			 return true;
		}
		
		return false;
		
	}
	
	public static boolean  isDateDiffBig(Date end,Date start) throws DAOSystemException
	{
		long divisor = (1000 * 3600 * 24);
		long diff= (int)(end.getTime()-start.getTime());///
		
		double numOfDays = diff/divisor;
		System.out.print("Difference is equal to: " + numOfDays);
		
		if(numOfDays>=14)
			return true;
		
		return false;
		
	}
	
	public static int  getDateDiff(Date end,Date start) throws DAOSystemException
	{
		long divisor = (1000 * 3600 * 24);
		long diff= (int)(end.getTime()-start.getTime());///
		
		double numOfDays = diff/divisor;
		System.out.print("Difference is equal to: " + numOfDays);
		
		
		return (int)numOfDays;
	}	
}

